package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Salary;
import com.example.demo.entity.result.NameValue;
import com.example.demo.entity.result.Point;
import com.example.demo.entity.result.Scatter;
import com.example.demo.mapper.SalaryMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/***
 ** Create by Wedream on 2021/7/16
 */
@RestController
@RequestMapping("/salary")
public class SalaryController {

    @Resource
    SalaryMapper salaryMapper;

    @PostMapping  //增
    public Result<?> save(@RequestBody Salary salary) {
        salaryMapper.insert(salary);
        return Result.success();
    }

    @DeleteMapping("/{id}") //删
    public Result<?> delete(@PathVariable long id) {
        salaryMapper.deleteById(id);
        return Result.success();
    }

    @GetMapping //查
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "200") Integer pageSize,
                              @RequestParam(defaultValue = "",required = false) String search) {

        LambdaQueryWrapper<Salary> wrapper = Wrappers.<Salary>lambdaQuery();
        if(StrUtil.isNotBlank(search)) {
           wrapper.like(Salary::getCity,search);
        }
        Page<Salary> salaryPage = salaryMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        List<Salary> records = salaryPage.getRecords();
        List<Scatter> scatters = new ArrayList<>();

        for(int i=0;i<records.size();i+=20) {
            Scatter scatter=new Scatter(records.get(i).getCity(),new ArrayList<>());
            scatters.add(scatter);
        }

        for(int i=0;i<records.size();i++) {
            String city=records.get(i).getCity();
            Double x0=records.get(i).getYear0();
            Double y0=records.get(i).getSalary0()*1.0;
            Double x1=records.get(i).getYear1to3();
            Double y1=records.get(i).getSalary1to3()*1.0;
            Double x2=records.get(i).getYear3to5();
            Double y2=records.get(i).getSalary3to5()*1.0;
            Double x3=records.get(i).getYear5to10();
            Double y3=records.get(i).getSalary5to10()*1.0;
            for(int j=0;j<scatters.size();j++) {
                if(city.equals(scatters.get(j).getCity())) {
                    scatters.get(j).getPoints().add(new Point(x0,y0));
                    scatters.get(j).getPoints().add(new Point(x1,y1));
                    scatters.get(j).getPoints().add(new Point(x2,y2));
                    scatters.get(j).getPoints().add(new Point(x3,y3));
                }
            }
        }

//        System.out.println(scatters.size());
//        System.out.println(scatters);

        Page<Scatter> scatterPage = new Page();  // 创建Scatter类型的Page,前端需要该类型的数据
        scatterPage.setRecords(scatters);
        scatterPage.setTotal(salaryPage.getTotal());
        scatterPage.setSize(salaryPage.getSize());
        scatterPage.setCurrent(salaryPage.getCurrent());
        scatterPage.setOrders(salaryPage.getOrders());
        scatterPage.setPages(salaryPage.getPages());


        return Result.success(scatterPage);
    }


    @PutMapping //改
    public Result<?> update(@RequestBody Salary salary) {
        salaryMapper.updateById(salary);
        return Result.success();
    }
}
